package com.zy.dingdong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.zy.dingdong.R;
import com.zy.dingdong.activity.BaseActivity;
import com.zy.dingdong.entity.Friend;
import com.zy.dingdong.entity.Message;
import com.zy.dingdong.entity.MessageItemView;
import com.zy.dingdong.entity.Owner;

import java.util.ArrayList;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {

    private Context context;
    private ArrayList<Message> messageList;
    private Friend friend;
    private Owner owner;

    static class ViewHolder extends RecyclerView.ViewHolder{

        LinearLayout leftLayout;
        LinearLayout rightLayout;

        /****/
        //左边图片文件layout
        LinearLayout leftImgLayout;
        ImageView leftImgFile;
        ImageView leftImgHeading;

        //右边图片文件layout
        LinearLayout rightImgLayout;
        ImageView rightImgFile;
        ImageView rightImgHeading;

        //layout集合
        ArrayList<LinearLayout> layouts;
        /****/

        TextView leftMsg;
        TextView rightMsg;
        ImageView rightImg;
        ImageView leftImg;

        public ViewHolder(View view){
            super(view);
            layouts = new ArrayList<>();
            //左边文本信息布局
            leftLayout = view.findViewById(R.id.left_layout);
            leftMsg = view.findViewById(R.id.left_msg);
            leftImg = view.findViewById(R.id.left_msg_img);
            layouts.add(leftLayout);
            /**右边文本信息布局**/
            rightLayout = view.findViewById(R.id.right_layout);
            rightMsg = view.findViewById(R.id.right_msg);
            rightImg = view.findViewById(R.id.right_msg_img);
            layouts.add(rightLayout);

            //左边图片信息布局
            leftImgLayout = view.findViewById(R.id.left_img_layout);
            leftImgFile = view.findViewById(R.id.left_img_file);
            leftImgHeading = view.findViewById(R.id.left_img_heading);
            layouts.add(leftImgLayout);

            //右边图片信息布局
            rightImgLayout = view.findViewById(R.id.right_img_layout);
            layouts.add(rightImgLayout);
            rightImgFile = view.findViewById(R.id.right_img_file);
            rightImgHeading = view.findViewById(R.id.right_img_heading);

            /****/
        }
        /****/
        //选取合适的布局，设置为可见
        public void setVisibility(LinearLayout layout){
            for (LinearLayout layout1: layouts){
                if (layout1==layout){
                    layout1.setVisibility(View.VISIBLE);
                }else {
                    layout1.setVisibility(View.GONE);
                }
            }
        }
        /****/
    }


    public MsgAdapter(Context context, MessageItemView messageItemView){
        this.context = context;
        this.messageList = messageItemView.messages;
        this.friend = messageItemView.friend;
        this.owner = messageItemView.owner;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);

        return new ViewHolder(view);
    }
    //这里需要重新写，结合数据库
    /****/
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Message message = messageList.get(position);
        switch (message.getMessageType()){
            //1为文本消息
            case 1:
                //如果发送方为本人
                if (message.getSenderNetId() == owner.getNetId()){
                    //将右布局设置为可见
                    holder.setVisibility(holder.rightLayout);
                    holder.rightMsg.setText(message.getContent());
                    //加载头像
                    Glide.with(context)
                            .load(BaseActivity.NGINX_SERVER_URL+"/"+owner.getImageBase64())
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(holder.rightImg);
                }
                //如果该校息发送方为好友，咋左布局设置为可见
                else {
                    holder.setVisibility(holder.leftLayout);
                    holder.leftMsg.setText(message.getContent());
                    Glide.with(context)
                            .load(BaseActivity.NGINX_SERVER_URL+"/"+friend.getHeadImage())
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(holder.leftImg);

                }
                break;
            //2为图像消息
            case 2:
                //获取到图片对象的URL
                String msgImage = message.getContent();
                //如果该校息发送方友为本人，则设置右图片布局可见
                if (message.getSenderNetId()== owner.getNetId()){
                    holder.setVisibility(holder.rightImgLayout);
                    //头像
                    Glide.with(context)
                            .load(BaseActivity.NGINX_SERVER_URL+"/"+owner.getImageBase64())
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(holder.rightImgHeading);
                    //文件
                    Glide.with(context)
                            .load(BaseActivity.NGINX_SERVER_URL+"/"+msgImage)
                            .into(holder.rightImgFile);
                }
                //如果该校息发送方友为好友，则设置左图片布局可见
                else {
                    holder.setVisibility(holder.leftImgLayout);
                    //头像
                    Glide.with(context)
                            .load(BaseActivity.NGINX_SERVER_URL+"/"+friend.getHeadImage())
                            .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                            .into(holder.leftImgHeading);
                    //文件
                    Glide.with(context)
                            .load(BaseActivity.NGINX_SERVER_URL+"/"+msgImage)
                            .into(holder.leftImgFile);
                }
                break;
            //3为系统消息
            case 3:
                break;
            default:
                break;
        }
        /****/
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }
}
